package middleware

import (
	"gitee.com/krio/ginc/example/core/exerr"
	"gitee.com/krio/ginc/example/router/output"
	"github.com/click33/sa-token-go/stputil"
	"github.com/gin-gonic/gin"
)

// 参数名称
const (
	MiddlewareKeyAdminAuth = "AdminAuth"           // 中间件标识
	LADMIN_ACCESS_TOKEN    = "Ladmin-Access-Token" // header字段
	LOGIN_ID               = "loginid"             // 登录标识
)

// AdminAuth admin后台鉴权
func AdminAuth(ctx *gin.Context) {
	token := ctx.Request.Header.Get(LADMIN_ACCESS_TOKEN)
	if token == "" {
		output.Response(ctx, "admin", exerr.TokenEmpty)
		ctx.Abort()
		return
	}
	err := stputil.CheckLogin(token)
	if err != nil {
		output.Response(ctx, "admin", exerr.TokenExpired.WithSubmsg(err.Error()))
		ctx.Abort()
		return
	}
	ctx.Set(claimsParam, LOGIN_ID)

	ctx.Next()
}
